package com.itheima.controller;

import com.itheima.pojo.Clazz;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;

    //分页查询班级信息
    @GetMapping
    public Result list(ClazzQueryParam clazzQueryParam){
        log.info("分页查询班级信息，参数：{}",clazzQueryParam);
         PageResult<Clazz> pageResult = clazzService.list(clazzQueryParam);
        return Result.success(pageResult);
    }

    //添加班级信息
    @PostMapping
    public Result save(@RequestBody Clazz clazz){
        log.info("添加班级信息，参数：{}",clazz);
        clazzService.save(clazz);
        return Result.success();
    }

    //根据id查询班级信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("根据id查询班级信息，参数：{}",id);
        Clazz clazz = clazzService.getById(id);
        return Result.success(clazz);
    }

    //修改班级
    @PutMapping
    public Result update(@RequestBody Clazz clazz){
        log.info("修改班级信息，参数：{}",clazz);
        clazzService.update(clazz);
        return Result.success();
    }

    //根据id删除班级
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id){
        log.info("删除班级信息，参数：{}",id);
        clazzService.delete(id);
        return Result.success();
    }

    //查询所有班级接口
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有班级信息");
        List<Clazz> clazzList =  clazzService.list();
        return Result.success(clazzList);
    }

}
